import router from './router'
// import { getToken } from './utils/auth'
import store from '@/store'
import { getSessionStorage } from '@/utils'

//前端访问域名

// const appId = 'wx38cf1142f2ed8ac5'
// const domian = 'http://lky.gzbigbang.net'
const appId = 'wx5878e8ad9e47c354'
const domian = 'http://test2.liukeyi.vip' // 会易佳
// const appId = 'wx9cfd48817f5dcb12'
// const domian = 'http://gzhengda.liukeyi.vip' // 广州恒大
// const appId = 'wx005810e08f01d730'
// const domian = 'http://jiahua.liukeyi.vip' // 嘉华
// const appId = 'wx255b7e98281b8094'
// const domian = 'http://jshc.liukeyi.vip' // 嘉尚
// const appId = 'wx14c0faa1c744ca14'
// const domian = 'http://zchd.liukeyi.vip' // 增城恒大
// const appId = 'wx8391bb747aa01211'
// const domian = 'http://huiyijia2.liukeyi.vip' // 会议佳商旅
// const appId = 'wx692d66685884412f'
// const domian = 'http://qyhd.liukeyi.vip' // 清远恒大
// const appId = 'wx5878e8ad9e47c354'
// const domian = 'http://test2.liukeyi.vip' // 留客易
// const appId = 'wx4f689dda79c323a8'
// const domian = 'http://xiyu.liukeyi.vip' // xiyu
// const appId = 'wx7f693d8d6942277a'
// const domian = 'http://gzds.liukeyi.vip' // 广州大厦
// const appId = 'wx208bab910a6618ab'
// const domian = 'http://hms.liukeyi.vip' // 轰谧斯
// const appId = 'wx38cf1142f2ed8ac5'
// const domian = 'http://lky.gzbigbang.cn' // 奇点
// const appId = 'wx122a9dbe1c77690e'
// const domian = 'http://fhc.liukeyi.vip' // 凤凰城
// const appId = 'wx592a0422130d201d'
// const domian = 'http://hj.liukeyi.vip' // 华距
// const appId = 'wx5d6b4e17fb008ba4'
// const domian = 'http://hyt.liukeyi.vip' // 会易通
// const appId = 'wx592a0422130d201d'
// const domian = 'http://hjtf.liukeyi.vip' // 华钜君悦
// const appId = 'wx6e40bbe949543126'
// const domian = 'http://hgtf.liukeyi.vip' // 韩冠
// const appId = 'wxdd6a3477c8060f17'
// const domian = 'http://kgtf.liukeyi.vip' // 空港
// const appId = 'wx000c1fee46d7aebb'
// const domian = 'http://sjhk.liukeyi.vip' // 四季花开

router.beforeEach( async (to, from, next) => {
  const ua = navigator.userAgent.toLowerCase()
  // const info = '{"comAddress":null,"comName":null,"comPhoto1":null,"comPhoto2":null,"comUserName":null,"createTime":null,"deleteStaff":null,"deleteTime":null,"memoName":"子豪","openId":"oV-kK1FaoIiugYw-r4yNZJN_mkYA","operName":null,"phone":"188888888099","salesman":2,"shareCount":null,"shareSum":null,"state":null,"telphone":null,"updateStaff":null,"updateTime":null,"userCode":1006,"userIcon":"http://thirdwx.qlogo.cn/mmopen/vi_32/GQgkkWjYbd5K3IxiclibCXy0jZF8K6V1ZOcKDEWGIwcZaUwQQyCa0PHAg4Xib2D3bnMMWUUSOHPXk5l4OmLdf9Biag/132","userId":"e637f9c6-fa65-418b-a9d2-301fe840de92","userLeveOfflStr":null,"userLevel":1,"userLevelStr":null,"userName":"*豪* ҉҉҉҉҉҉҉҉","userNickname":"8J+UpeixqvCflKUg0onSidKJ0onSidKJ0onSiQ==","userNicknameStr":"🔥豪🔥 ҉҉҉҉҉҉҉҉","userPoint":0,"userSalesman":null,"userType":1}'
  // const info = '{"comAddress":null,"comName":null,"comPhoto1":null,"comPhoto2":null,"comUserName":null,"createTime":null,"deleteStaff":null,"deleteTime":null,"memoName":"子豪","openId":"o17vht5q2uja5iuCxvgw1LmG1k-w","operName":null,"phone":"18888888888","salesman":2,"shareCount":null,"shareSum":null,"state":null,"telphone":null,"updateStaff":null,"updateTime":null,"userCode":1015,"userIcon":"http://thirdwx.qlogo.cn/mmopen/vi_32/SNrvEjqzhXKWmNxibkMLSeQh5DRs6UnWPZudO72Vciat5HribWNkyTTa9BR8e1bQzNbO7yChTpbKhH9YPLEicFKtwg/132","userId":"1f0aedb3-1d7a-4c3c-b89b-065fccf8ae37","userLeveOfflStr":null,"userLevel":1,"userLevelStr":null,"userName":"*豪* ҉҉҉҉҉҉҉҉","userNickname":"8J+UpeixqvCflKUg0onSidKJ0onSidKJ0onSiQ==","userNicknameStr":"🔥豪🔥 ҉҉҉҉҉҉҉҉","userPoint":0,"userSalesman":null,"userType":1}'
  // const userInfo = JSON.parse(info)
  // store.commit('SET_USERINFO', userInfo)
  // next()

  if(ua.indexOf('micromessenger') !== -1){
    const info = getSessionStorage('userInfo')
    console.info('info', info)
    if (info) {
      let userInfo = JSON.parse(info)
      await store.commit('SET_USERINFO', userInfo)
      next()
    } else {
      console.info('to.query.code', to)
      if (to.query.code) {
        await store.dispatch('SetUserInfoAct', { code: to.query.code })
        next()
      } else {
        await store.commit('SET_USERINFO', {})
        return window.location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appId}&redirect_uri=${domian}${to.fullPath}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect`
      }
    }
  }else{
    alert('请用微信打开');
    next()
  }

  // const info = getSessionStorage('userInfo')
  // let userInfo = info ? JSON.parse(info) : {}
  // store.commit('SET_USERINFO', userInfo)
  // next()

  // const access_token = getToken()
  // // 如果有token
  // if (access_token) {
  //   if (to.path === '/login') {
  //     next({ path: '/home' })
  //   } else {
  //     next()
  //   }
  // } else {
  //   if (whiteList.indexOf(to.path) !== -1) {
  //     next()
  //   } else {
  //     next(`/login?redirect=${to.path}`) // 否则全部重定向到登录页
  //   }
  // }
})

// router.afterEach(() => {
//   next()
// })
